package com.iecampus.adapter;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.iecampus.activity.R;
import com.iecampus.moldel.Notification;
import com.iecampus.utils.VolleyUtil;

public class NotificationAdapter extends BaseAdapter {
	private List<Notification> data;
	private Context context;
	public final static int REPORT = 1;
	public final static int NOTICE = 0;

	public NotificationAdapter(List<Notification> data, Context context) {
		this.context = context;
		this.data = data;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public int getItemViewType(int position) {
		int type = data.get(position).getType();
		if (type == NOTICE) {
			return NOTICE;
		} else {
			return REPORT;
		}
	}

	@Override
	public int getViewTypeCount() {
		return 2;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view;
		ViewHolder holder;
		view = convertView;
		if (view == null) {
			int type = getItemViewType(position);
			switch (type) {
			case NOTICE:
				view = LayoutInflater.from(context).inflate(
						R.layout.notification_listviewitem_notice, parent,
						false);
				break;
			case REPORT:
				view = LayoutInflater.from(context).inflate(
						R.layout.notification_listviewitem_report, parent,
						false);
				break;
			default:
				break;
			}
			holder = new ViewHolder();
			holder.action = (TextView) view.findViewById(R.id.action);
			holder.content = (TextView) view.findViewById(R.id.content);
			holder.icon = (ImageView) view.findViewById(R.id.notification_icon);
			holder.userimg = (ImageView) view.findViewById(R.id.userimg);
			holder.username = (TextView) view.findViewById(R.id.username);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		Notification notification = data.get(position);
		if (notification.getType() == NOTICE) {
			bindNoticeView(holder, notification);
		} else {
			bindReportView(holder, notification);
		}
		return view;
	}

	private void bindReportView(ViewHolder holder, Notification notification) {
		if (notification.isUnread()) {
			holder.icon.setImageResource(R.drawable.report_read);
		} else {
			holder.icon.setImageResource(R.drawable.report_unread);
		}
		String content = getKey(notification.getJsonInfo(), "content");
		String username = getKey(notification.getJsonInfo(), "username");
		String url = getKey(notification.getJsonInfo(), "imgUrl");
		VolleyUtil.setUserImage(holder.userimg,
				context.getString(R.string.hostAddress) + url, context);
		holder.username.setText(username);
		holder.content.setText(content);
	}

	private void bindNoticeView(ViewHolder holder, Notification notification) {
		if (notification.isUnread()) {
			holder.icon.setImageResource(R.drawable.notification_read);
		} else {
			holder.icon.setImageResource(R.drawable.notification_unread);
		}
		String content = getKey(notification.getJsonInfo(), "content");
		String username = getKey(notification.getJsonInfo(), "username");
		holder.username.setText(username);
		holder.content.setText(content);
	}

	private String getKey(String jsoninfo, String key) {
		JSONObject json;
		try {
			json = new JSONObject(jsoninfo);
			String str = json.getString(key);
			return str;
		} catch (JSONException e) {
			e.printStackTrace();
			return "";
		}
	}

	class ViewHolder {
		private ImageView userimg;
		private TextView username;
		private TextView action;
		private TextView content;
		private ImageView icon;
	}
}
